home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************
- * *
- * Sound Server version 0.0.1 by Ken Hollis <khollis@bitgate.com> *
- * Copyright (C) 1995-1996, Bitgate Software *
- * *
- * This sound server is released under the Linux Gnu Public License. This *
- * program is not for resale, and may not be modified without releasing *
- * patches. *
- * *
- *****************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <fcntl.h>
- #include <pwd.h>
- #include <ctype.h>
- #include <sys/signal.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/errno.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
-
- #include "config.h"
-
- #define SERVER_PORT 8000
-
- int server_socket, sockets[16];
- fd_set readset, activeset;
-
- void Server_Initialize(void)
- {
- struct protoent *p;
- struct sockaddr_in sin;
- int i;
-
- for(i = 0; i < 16; i++)
- sockets[i] = -1;
-
- bzero((char *) &sin, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
-
- if ((sin.sin_port = htons(SERVER_PORT)) == 0) {
- printf("Sound server cannot initialize - port 8000 cannot be found!\n");
- exit(0);
- }
-
- if ((p = getprotobyname("tcp")) == 0) {
- printf("Sound server cannot initialize - Protocol \"tcp\" unknown.\n");
- exit(0);
- }
-
- if ((server_socket = socket(AF_INET, SOCK_STREAM, p->p_proto)) < 0) {
- printf("Sound server cannot initialize - socket creation impossible\n");
- exit(0);
- }
-
- if (bind(server_socket, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- printf("Sound server cannot initialize - could not bind!\n");
- exit(0);
- }
-
- if (listen(server_socket, 5) < 0) {
- printf("Sound server cannot initialize - could not listen!\n");
- exit(0);
- }
-
- printf("Sound server 0.0.1 started.\n");
- }
-
- int add_connection(int sock)
- {
- int i;
-
- for(i = 0; i < 16; i++)
- if (sockets[i] == -1) {
- sockets[i] = sock;
- return(1);
- }
-
- return(0);
- }
-
- void remove_connection(int sock)
- {
- int i;
-
- for(i = 0; i < 16; i++)
- if (sockets[i] == sock) {
- sockets[i] = -1;
- return;
- }
- }
-
- void handle_data(char *cmd)
- {
- char command, buffer[8000];
-
- bzero(buffer, 8000);
- sscanf(cmd, "%c%s\r\n", &command, buffer);
-
- if (command == 'P') {
- /* Play a Sun Audio sound file, if this is one. */
- if (strstr(buffer, ".au")) {
- int f1, f2;
- char bleah[512];
-
- f1 = open("/dev/audio", O_WRONLY);
- f2 = open(buffer, O_RDONLY);
-
- while((read(f2, &bleah, 512)) > 0)
- write(f1, &bleah, 512);
-
- close(f1);
- close(f2);
- }
-
- /* Play Microsoft WAV file, if this is one. */
- if (strstr(buffer, ".wav")) {
- char fn[160];
-
- bzero(fn, 160);
- sprintf(fn, "%s %s", WAVFILE, buffer);
- system(fn);
- }
- }
- }
-
- void handle_client(int i)
- {
- int size;
- char buffer[8192];
-
- bzero(buffer, 8192);
- size = read(sockets[i], buffer, 8192);
-
- if (size <= 0) {
- (void) shutdown(sockets[i], 2);
- close(sockets[i]);
-
- FD_CLR(sockets[i], &readset);
- FD_CLR(sockets[i], &activeset);
-
- remove_connection(i);
- return;
- } else {
- handle_data(buffer);
- }
- }
-
- void Server_Loop(void)
- {
- int fds = getdtablesize(), i;
- struct sockaddr_in sin;
-
- FD_ZERO(&activeset);
- FD_SET(server_socket, &activeset);
-
- while(1) {
- struct timeval t;
-
- t.tv_sec = 0;
- t.tv_usec = 100;
-
- bcopy(&activeset, &readset, sizeof(readset));
-
- if (select(fds, &readset, NULL, NULL, &t) < 0) {
- fprintf(stderr, "(Sound server reports a select error)\n");
- }
-
- if (FD_ISSET(server_socket, &readset)) {
- int addrlen, newsock;
-
- addrlen = sizeof(struct sockaddr_in);
- newsock = accept(server_socket, (struct sockaddr *) &sin, &addrlen);
-
- if (newsock < 0) {
- fprintf(stderr, "(Sound server reports a socket accept error)\n");
- } else {
- FD_SET(newsock, &activeset);
-
- if (!add_connection(newsock)) {
- (void) shutdown(newsock, 2);
- close(newsock);
- FD_CLR(newsock, &activeset);
- FD_CLR(newsock, &readset);
- }
- }
- }
-
- for(i = 0; i < 16; i++)
- if (sockets[i] != -1)
- if (FD_ISSET(sockets[i], &readset))
- handle_client(i);
- }
- }
-
- void main(int argc, char *argv[])
- {
- Server_Initialize();
- Server_Loop();
- }
-